################################################
# Data Pre-Processing Code
# Coercion and the Credibility of Assurances
# Matthew Cebul, Allan Dafoe, and Nuno Monteiro
################################################


setwd("/Users/mcebul/Dropbox/Co-Authored Projects/Assurances in IR/Submissions/Replication Files")
set.seed(1991)


##########
# Load Data
##########

data <- read.csv("Assurances_JOP_Raw.csv")
data$done <- as.numeric(as.character(data$PolParty)) # indicator for completed survey
data <- data[!(is.na(data$done)),] # drop unfinished surveys
length(data$InfCons) #1453 finished 

##########
# Drop Inattentive Respondents
##########

# PSIDs if finished in < 4 mins
data$Duration..in.seconds. <- as.numeric(as.character(data$Duration..in.seconds.))

# PSIDs if read Reputation treatment in < 5 seconds
data$TimingRep_Page.Submit <- as.numeric(as.character(data$TimingRep_Page.Submit))

# Drop Them
data <- data[data$Duration..in.seconds. > 240,]
data <- data[data$TimingRep_Page.Submit > 5,]
length(data$InfCons) # Full Sample = 1028 respondents

##########
# Dependent Variables
##########

# Resolve
data$Resolve1 <- as.numeric(as.character(data$Resolve1))
colnames(data)[colnames(data)=="Resolve1"] <- "Resolve"
# 0 = Comply; 1 = Bluff, 2 = Reject < 100 Casualties, 3 = Reject At Any Cost

# Threat Cred
data$Threat.Cred..1 <- as.numeric(as.character(data$Threat.Cred..1))
colnames(data)[colnames(data)=="Threat.Cred..1"] <- "CredThreat"  
# scale from 0-4, higher numbers = more credible threat

# Assurance Cred
data$Assurance.Cred..1 <- as.numeric(as.character(data$Assurance.Cred..1))
colnames(data)[colnames(data)=="Assurance.Cred..1"] <- "CredAssurance"
# scale from 0-4, higher numbers = more credible assurance


##########
# Independent Variables
##########

# President's Party 
data$party <- as.numeric(as.character(data$party)) - 1 
colnames(data)[colnames(data)=="party"] <- "GOPPresident"
# 0 = Democratic Pres., 1 = Republican Pres.

# Power
data$Power <- as.numeric(as.character(data$Power)) - 1
# 0 = U.S. dominance, 1 = China dominance

# Reputation
data$Reputation <- as.numeric(as.character(data$Reputation))
data$Reputation <- replace(data$Reputation, data$Reputation==2, 0)
# 0 = No Reputation for Restraint, 1 = Reputation for Restraint


##########
# Controls
##########

# Age
data$Age <- as.numeric(as.character(data$Age))

# Gender
data$Male <- ifelse(data$Gender == 1, 1, 0)

# Race
data$Race[data$Race == 8] <- NA
data$Race <- replace(data$Race, data$Race==5 | data$Race==6, 7)
# 1 = Caucasian
# 2 = African-American
# 3 = Asian
# 4 = Hispanic
# 7 = Other

# Respondent Party
data$RParty <- as.numeric(as.character(data$PolParty)) - 1
data$Dem <- ifelse(data$RParty == 0, 1, 0)
data$Rep <- ifelse(data$RParty == 2, 1, 0)
data$Ind <- ifelse(data$RParty == 1, 1, 0)

# Political Knowledge (reading about foreign affairs)
data$PolKnow[data$PolKnow == -98] <- NA
data$PolKnow <- as.numeric(as.character(data$PolKnow))
# 0 = low pol knowledge, 4 = high pol knowledge

# Education
data$Education <- as.numeric(as.character(data$Education)) - 1
data$Education[data$Education == -99] <- NA
colnames(data)[colnames(data)=="Education"] <- "Edu"
# 0 = low edu, 7 = high edu

# Income
data$Income <- as.numeric(as.character(data$Income)) - 1
data$Income[data$Income == -99] <- NA
# 0 = low income, 10 = high income

# Mil Background
data$MilExp <- as.numeric(as.character(data$MilExp))
data$MilExp[data$MilExp == -98] <- NA
# 0 = no mil service, 1 = mil service

# Expected Costs of War
data$Cost <- as.numeric(as.character(data$Cost)) - 1
# 0 = 0-24 deaths, 4 = greater than 1000


##########
# Remove Clutter (for tidiness)
##########
data <- subset(data, select = -c(StartDate, EndDate, Duration..in.seconds., InfCons, Status, 
        Progress, Finished, RecordedDate, psid, pid, DistributionChannel, UserLanguage, 
        Timing1_First.Click, Timing1_Last.Click, Timing1_Page.Submit, Timing1_Click.Count,
        Timing1.5_First.Click, Timing1.5_Last.Click, Timing1.5_Page.Submit, Timing1.5_Click.Count,
        Timing2_First.Click, Timing2_Last.Click, Timing2_Page.Submit, Timing2_Click.Count, 
        TimingPower_First.Click, TimingPower_Last.Click, TimingPower_Click.Count, 
        TimingPower_Page.Submit, TimingRep_First.Click, TimingRep_Last.Click, TimingRep_Click.Count,
        TimingRep_Page.Submit, Timing7_First.Click, Timing7_Last.Click, Timing7_Click.Count,
        Timing7_Page.Submit, Resolve2, Threat.Cred..2, Assurance.Cred..2, Cost, Attn1, Attn2,
        USstate, Pol7scale, PolParty, StrDem, StrRep, DemRep, feedback, done))

write.csv(data, "Assurances_JOP_Clean.csv", row.names=FALSE)

